#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
bool flag[100100];
long long a[100100];
int dst[100100];
long long  ans[100100];
int fa[100100];
long long sum[100100];
int n;
int find(int x)
{
	if(fa[x]==x) return x;
	else return find(fa[x]);
}
int main()
{
	scanf("%d",&n);
	for(int i = 1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		fa[i]=i;
	} 
	for(int i = 1;i<=n;i++) scanf("%d",&dst[i]);
	for(int i = n;i>=1;i--)
	{
		int t = dst[i];
		sum[t] = a[t];
		if(flag[t-1])
		{
			int t1 = find(t);
			int t2 = find(t-1);
			fa[t1] = t2;
			sum[t2] += sum[t1]; 
		}
		if(flag[t+1])
		{
		//	printf("+++++\n");
			int t1 = find(t);
			int t2 = find(t+1);
			fa[t1] = t2;
			sum[t2] += sum[t1]; 
		}
		
		ans[i-1] = max(sum[find(t)],ans[i]);
		//printf("%d %d\n",sum[find(t)],ans[i]);
		//printf("%d\n",ans[i]);
		flag[t]=1;
		//printf("*****\n");
	}
	for(int i = 1;i<=n;i++) printf("%I64d\n",ans[i]);
}
